"""
Edit file hatwidget.kv for editing the background or other parameters of UI.

To use it, you should import this file (add 'from hat_widget import HatWidget'
in your program).
Your root widget should inherit from HatWidget.

ATTENTION: .kv file for screen should NOT contain its own color of background.
"""

import datetime

from kivy.logger import Logger
from kivy.properties import ObjectProperty
from kivy.uix.popup import Popup
from kivy.uix.boxlayout import BoxLayout


def get_time():
    return unicode(datetime.datetime.now().strftime('%m.%d_%H.%M.%S'))


class HatPopup(Popup):
    def open(self, **kwargs):
        Logger.debug((u'LOG: (' + get_time() + u') ' + u'Popup \'' +
                      self.title + u'\' opened').encode('utf-8'))
        super(HatPopup, self).open(**kwargs)

    def on_dismiss(self, **kwargs):
        Logger.debug((u'LOG: (' + get_time() + u') ' + u'Popup \'' +
                      self.title + u'\' dismissed').encode('utf-8'))


def open_ok_popup(title, text, widget):
    from ui.fittable_widgets import FittableButton, FittableLabel

    button_ok = FittableButton(text=_(u'OK'), size_hint_y=None,
                               height=widget.height / 9)
    label = FittableLabel(text=text, size_hint=(1, 1))

    content = BoxLayout(orientation='vertical', size_hint=(1, 1))
    content.add_widget(label)
    content.add_widget(button_ok)

    popup = HatPopup(content=content,
                     title=title,
                     size_hint=(None, None),
                     size=(widget.width / 1.5, widget.height / 1.5))
    button_ok.bind(on_release=popup.dismiss)
    popup.open()


def open_yes_no_popup(widget, title, text, yes_action=None, no_action=None):
    from ui.fittable_widgets import FittableButton, FittableLabel

    button_yes = FittableButton(text=_(u'YES'), size_hint_y=None,
                                height=widget.height / 9)
    button_no = FittableButton(text=_(u'NO'), size_hint_y=None,
                               height=widget.height / 9)
    button_box = BoxLayout(orientation='horizontal', size_hint=(1, 1))
    button_box.add_widget(button_yes)
    button_box.add_widget(button_no)
    label = FittableLabel(text=text, size_hint=(1, 1))

    content = BoxLayout(orientation='vertical', size_hint=(1, 1))
    content.add_widget(label)
    content.add_widget(button_box)
    popup = HatPopup(content=content,
                     title=title,
                     size_hint=(None, None),
                     size=(widget.width / 1.5, widget.height / 1.5))

    if yes_action is not None:
        button_yes.bind(on_release=yes_action)
    if no_action is None:
        button_no.bind(on_release=no_action)
    button_yes.bind(on_release=popup.dismiss)
    button_no.bind(on_release=popup.dismiss)

    popup.open()


class HatWidget(BoxLayout):
    presenter = ObjectProperty(None)

    def __init__(self, presenter, **kwargs):
        super(HatWidget, self).__init__(**kwargs)
        self.presenter = presenter

    def go_back(self):
        pass

    def update(self):
        pass
